<!-- 增加页面开始 -->


[#macro fullreductionAddView]
<div class="model-form">
	<el-page-header @back="goBack" content="增加满减活动">
	</el-page-header>
	<div class="model-content">
		<el-form ref="ruleForm" :rules="rules" :model="form" label-width="160px">
			<el-row :gutter="10">
				<el-col :span="20">
					<el-form-item label="名称" size="mini" prop="name">
						<el-input v-model="form.name" name="name">
						</el-input>
					</el-form-item>

				</el-col>
				<el-col :span="20">
					<el-form-item label="商家" size="mini" prop="shop">
						<el-select style="width: 100%;" v-model="form.shop" filterable clearable placeholder="请选择">
							<el-option v-for="item in shopOptions" :key="item.id" :label="item.name" :value="item.id">
							</el-option>
						</el-select>
					</el-form-item>

				</el-col>
				<el-col :span="20">
					<el-form-item label="活动时间" size="mini" prop="sendTime">
						<el-date-picker v-model="form.sendTime" type="datetimerange" value-format="yyyy-MM-dd HH:mm"
							placeholder="选择日期">
						</el-date-picker>
					</el-form-item>

				</el-col>
				<el-col :span="20">
					<el-form-item label="参与设置" size="mini" prop="fullReductionWay">
						<el-radio v-model="form.fullReductionWay" label="all">全部商品参与</el-radio>
						<el-radio v-model="form.fullReductionWay" label="part">限时折扣商品不参与</el-radio>
					</el-form-item>
				</el-col>
				<el-col :span="20">
					<el-form-item size="mini" v-for="(item, index) in form.items" :label="'满减项' + index" :key="item.key">
						满
						<el-input v-model="form.items[index].fullMoney" style="width: 100px;"></el-input>
						减
						<el-input v-model="form.items[index].reduceMoney" style="width: 100px;"></el-input>
						<el-button @click.prevent="removeItem(index)">删除</el-button>
						<el-button @click="addItem" type="primary" size="mini">新增属性</el-button>
					</el-form-item>

				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24" style="text-align: right;padding-bottom: 20px;padding-top: 20px;">
					<el-button @click="goBack">取消</el-button>
					<el-button type="primary" @click="add">确定</el-button>
				</el-col>
			</el-row>
		</el-form>
	</div>
</div>
[/#macro]

[#macro fullreductionAddConfig]
<script type="text/x-template" id="my-fullreduction-add">
	[@fullreductionAddView /]
</script>
<script type="text/javascript">
	//添加组件开始
	var fullreductionAddConfig = {};
	fullreductionAddConfig.template = "#my-fullreduction-add";
	fullreductionAddConfig.methods = config.basicMethod();
	fullreductionAddConfig.data = function () {
		return {
			form: {
				name: '',
				shop: '',
				beginTime: '',
				endTime: '',
				fullReductionWay: 'all',
				sendTime: [],
				items: [{
					fullMoney: '',
					reduceMoney: "",
				}],
			},
			rules: {
				name: [
					{ required: true, message: '请输入名称', trigger: 'blur' }
				],
				shop: [
					{ required: true, message: '请输入商家', trigger: 'blur' }
				],
			}
		}
	};
	fullreductionAddConfig.mounted = function () {
	}
	fullreductionAddConfig.computed = {};
	fullreductionAddConfig.computed.shopOptions = function () {
		return this.$store.state.shopOptions;
	};

	fullreductionAddConfig.methods.addItem = function () {
		this.form.items.push({
			fullMoney: '',
			reduceMoney: "",
			key: Date.now()
		});
	}
	fullreductionAddConfig.methods.removeItem = function (item) {
		if (item > -1) {
			if(this.form.items.length>1){
				this.form.items.splice(item, 1)
			}else{
				this.$message.error("满减活动必须保留一项");
			}
		}
	};


	fullreductionAddConfig.methods.add = function () {
		var self = this;
		this.$refs["ruleForm"].validate(function (valid) {
			if (valid) {
				self.addData();
			} else {
				console.log('error submit!!');
				return false;
			}
		});
	}

	fullreductionAddConfig.methods.addData = function () {
		var self = this, data = this.form;
		if (this.deforeAddData) {
			this.deforeAddData();
		}
		if (this.form.sendTime!=null&&this.form.sendTime.length == 2) {
            this.form.beginTime = this.form.sendTime[0];
            this.form.endTime = this.form.sendTime[1];
			delete this.form.sendTime;
        }
		this.postJsonData("${siteurl}tenantRest/fullreduction/create.htm", data, function (res) {
			if (res.code == 0) {
				self.$message({
					message: '添加数据成功',
					type: 'success'
				});
				self.$router.go(-1);
			} else {
				self.$message.error(res.msg);
			}
		});
	}

	fullreductionAddConfig.destroyed = function () {
		console.log("destroyed add view ");

	}
	var fullreductionAddView = Vue.component('add-view', fullreductionAddConfig);


</script>
[/#macro]

<!-- 增加页面结束 -->